package com.example.fitnessservice.api.web;

import com.example.fitnessservice.module.pojo.Result;
import com.example.fitnessservice.service.EnterForCampService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @author feng
 * @version 1.0
 * @&#064;description: TODO
 * @&#064;date 2022/11/25 11:40
 */
@RestController
@RequestMapping("/camp")
public class CampCourseController {
    @Resource
    EnterForCampService enterForCampService;
    /*更新课程状态

     */
    @PutMapping()
    public Result findAllPlaceTeamCourse() {
        Result result = new Result();
        Date date = new Date();
        if (enterForCampService.updateCourseStatus(date)) {
            result.setCode(200);
            result.setMsg("更新状态成功");
        } else {
            result.setCode(500);
            result.setMsg("更新失败");
        }
        return result;
    }
    /*选课

     */
    @PostMapping("/{userId}/{courseId}/{courseCampIf}")
    public Result enterForCampService(@PathVariable Integer userId,
                                  @PathVariable Integer courseId,
                                  @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf == 0) {
            result.setCode(500);
            result.setMsg("当前服务只应用于训练营");
            return result;
        }
        if (enterForCampService.join(userId, courseId,courseCampIf)) {
            result.setCode(200);
            result.setMsg("选课成功,请准时参加");
        } else {
            result.setCode(500);
            result.setMsg("选课失败,课程余量不足");
        }
        return result;
    }
    /*
    查询是否可以选课
     */
    @GetMapping("/{userId}/{courseId}/{courseCampIf}")
    public Result getCourseInfo(@PathVariable Integer userId,
                                @PathVariable Integer courseId,
                                @PathVariable Integer courseCampIf) {

        Result result = new Result();
        if (courseCampIf == 0) {
            result.setCode(500);
            result.setMsg("当前服务只应用于训练营");
            return result;
        }
        if(enterForCampService.getCourseQuantity(courseId)==0){
            result.setCode(500);
            result.setMsg("课余量不足");
            return result;
        } else if (enterForCampService.latestTimeLimit(courseId)) {
            result.setCode(500);
            result.setMsg("开课前五分钟不能选课");
            return result;
        } else if (enterForCampService.getCourseInfo(userId, courseId, courseCampIf)) {
            result.setCode(500);
            result.setMsg("已选择该课");
            return result;
        } else {
            result.setCode(200);
            result.setMsg("可以选课");
        }
        return result;
    }

    /*
    退课,发送个人课程信息申请,针训练营课程
     */
    @DeleteMapping("/{userId}/{courseId}/{courseCampIf}")
    public Result dropCourse(@PathVariable Integer userId,
                             @PathVariable Integer courseId,
                             @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf == 0) {
            result.setCode(500);
            result.setMsg("当前服务只用于训练营");
            return result;
        }
//        是否符合退课条件
            if (enterForCampService.cancelCourse(userId, courseId,courseCampIf)) {
                result.setCode(200);
                result.setMsg("退课成功,祝你生活愉快");

        } else {
            result.setCode(500);
            result.setMsg("退课失败,当前时间不允许申请退课");
        }
        return result;
    }
    /*
    查询是否运行退课
     */
    @DeleteMapping("/{courseId}/{courseCampIf}")
    public Result whetherApply(@PathVariable Integer courseId,
                               @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf == 0) {
            result.setCode(500);
            result.setMsg("当前服务只用于训练营");
            return result;
        }

        if (enterForCampService.whetherApply(courseId)) {
            result.setCode(200);
            result.setMsg("允许申请退课");
        } else {
            result.setCode(500);
            result.setMsg("不允许申请退课");
        }
        return result;
    }
    /*
    查询课余量
     */
    @GetMapping("/{courseId}/{courseCampIf}")
    public Result getCourseQuantity(@PathVariable Integer courseId,
                                    @PathVariable Integer courseCampIf) {
        Result result = new Result();
        if (courseCampIf == 0) {
            result.setCode(500);
            result.setMsg("当前服务只应用于训练营");
            return result;
        }
        Integer courseQuantity=enterForCampService.getCourseQuantity(courseId);
        if (courseQuantity>0) {
            result.setCode(200);
            result.setMsg("课余量还有");
            result.setData(courseQuantity);
        } else {
            result.setCode(500);
            result.setMsg("课余量已满");
        }
        return result;
    }

}
